package org.flashbrighton.as3bots.bots
{
	import org.flashbrighton.as3bots.components.ChassisSize;
	import org.flashbrighton.as3bots.components.ExtraComponents;
	import org.flashbrighton.as3bots.events.BombSensorEvent;
	import org.flashbrighton.as3bots.events.CollectGemEvent;
	import org.flashbrighton.as3bots.events.GemSensorEvent;
	import org.flashbrighton.as3bots.events.HitBombEvent;
	import org.flashbrighton.as3bots.events.HitBotEvent;
	import org.flashbrighton.as3bots.events.HitWallEvent;
	import org.flashbrighton.as3bots.events.ScanBotEvent;
	import org.flashbrighton.as3bots.models.BotData;
	import org.flashbrighton.as3bots.models.GemPosition;
	import org.flashbrighton.as3bots.utils.MathUtils;
	
	public class MattsBot extends BaseBot
	{		
		private var collision:Boolean;
		private var uturn:Boolean;
		private var health:Number;
		private var targetGem:Boolean;

		public function MattsBot()
		{
			super(ExtraComponents.BOTSCANNER, 0xFF0000, ChassisSize.LARGE);
			enginePower = 1;
		}
		
		override public function onHitBot(event:HitBotEvent):void
		{
			collision = true;
			//max speed to run away or attack
			enginePower = 1;
			
			//if hit at the front,
			if(Math.abs(event.collisionPoint)<0.5){
				//turn toward bot
				steeringPower = event.collisionPoint*2;
			}
		}
		
		override public function onHitWall(event:HitWallEvent):void
		{
			collision = true;
			// si j'avance, alors je recule au ralenti en braquant à fond pour repartir tout droit et à fond au prochain hitWall
			if(enginePower>0){
				uturn = true;
				enginePower = -0.1;
				steeringPower = -MathUtils.sign(event.collisionPoint);
			}else{
				uturn = false;
				// go baby go ! 
				enginePower = 0.3;
				steeringPower = 0;
			}
		}		
		
		override public function update(data:BotData):void
		{		
			var loss:Number = health-data.health;
			health = data.health;
			if(collision){
				collision = false;
			}
		}
		
				
		private var currentTarget:String;
		override public function onScanBot(event:ScanBotEvent):void
		{
			if(event.botName!=currentTarget){
				currentTarget = event.botName;
				writeMessage("You are my next target '" + currentTarget + "' !");
			}
			if(!collision && !uturn && !targetGem){
				if(
					(event.distance>0.5 && Math.abs(event.direction)<0.3) ||
					(event.distance<=0.5 && Math.abs(event.direction)<0.1)){
					enginePower = 1;
				}else{
					enginePower = MathUtils.sign(enginePower)*0.1;
				}
				steeringPower = 3*event.direction;
			}			
		}
		
		override public function onGemSensor(event:GemSensorEvent):void
		{
			if(event.gemPositionArray.length>0){
				var gemPosition:GemPosition = event.gemPositionArray[0];
				for each (var gp:GemPosition in event.gemPositionArray) 
				{
					if(gp.gemBonus>gemPosition.gemBonus){
						gemPosition = gp;
					}else if(gp.gemBonus==gemPosition.gemBonus && gp.distance<gemPosition.distance){
						gemPosition = gp;
					}
				}
				targetGem = true;
				if(Math.abs(gemPosition.direction)<0.3){
					enginePower = 1;
				}else{
					enginePower = MathUtils.sign(enginePower)*0.1;
				}
				steeringPower = 4*gemPosition.direction;
			}else{
				targetGem = false;
			}
		}

		override public function onBombSensor(event:BombSensorEvent):void
		{
		}

		override public function onCollectGem(event:CollectGemEvent):void
		{
			writeMessage("Yeah ! Got a bonus of " + event.bonus);
			dropBomb();
		}
		
		override public function onHitBomb(event:HitBombEvent):void
		{
		}
		

	}
}